#!/usr/bin/env python3
# -*- coding: utf-8 -*-

import json
from generate.generator import BasicGenerator

import logging

'''
task = {
    'type': 'download_xx',               # 必填，任务类型
    'url': 'http://www.xx.com/id.html'   # 必填，url地址， get请将参数放到url中
    'file': './data/download_xx/id.html' # 选填, 是否要存成文件
    'duplicated': False,                 # 选填, False为不去重
    'duplicated_key': 'xx',              # 选填, 去重的key，一般不使用
    'method': 'post',                    # 选填, post接口用
    'params': {'a':'b', 'c':'d'}         # 选填, 传参数
    ...                                  # 其它自定义参数
    'data'                               # data 已经预留，不允许使用
    'expire_time'                        # 选填, 传去重过期时间
}

config = {
    'name'
    'mq'
    'stat'
    'proxy'
    'mdb'
    'analyze_type'
}
'''


# 机构募资事件详情
class SmtProjectGenerator(BasicGenerator):
    def get_project(self):
        sql1 = '''select fundId from smt_manage_fund_list;'''
        sql2 = '''select qkid from smt_manage_fund_survey_info;'''
        rows1 = self.mdb.query(sql1)
        rows2 = self.mdb.query(sql2)
        list = []
        try:
            for row1 in rows1:
                list.append(row1)
            for row2 in rows2:
                list.remove(row2)
        except Exception as e:
            logging.info(e)
        return list

    def gen_task(self, extra=None):
        projects = self.get_project()
        for project_id in projects:
            url = 'https://app.pedata.cn/PEDATA_APP_BACK/orgBasic/surveyInfo?qkid=' + str(project_id[0])  # 机构募资事件详情
            task = {
                'type': self.name,
                'url': url,
                'project_id': project_id[0],
                # 'cookie': cookie,
                # 'file': './data/download_qhee/{0}.html'.format(str(project_id[0]))
            }
            length = self.rds.rpush(self.queue_name, json.dumps(task))
            logging.info('{0} {1}'.format(length, task))
            self.stat.incr(self.stat_hash, 'gen_task')


# 机构募资事件列表
class SmtInfoListProjectGenerator(BasicGenerator):
    def get_project(self):
        # 机构募资事件列表 代码
        sql1 = '''select qkid from smt_survey_info;'''
        sql2 = '''select qkid from smt_manage_fund_info;'''
        rows1 = self.mdb.query(sql1)
        rows2 = set(self.mdb.query(sql2))
        list = []
        for row1 in rows1:
            list.append(row1)
        for row2 in rows2:
            list.remove(row2)
        return list

    def gen_task(self, extra=None):
        projects = self.get_project()
        for project_id in projects:
            url = 'https://app.pedata.cn/PEDATA_APP_BACK/orgBasic/manageFundInfo?start=0&limit=20&qkid=' + str(
                project_id[0])  # 机构募资事件列表
            task = {
                'type': self.name,
                'url': url,
                'project_id': project_id[0],
                # 'cookie': cookie,
                # 'file': './data/download_qhee/{0}.html'.format(str(project_id[0]))
            }
            length = self.rds.rpush(self.queue_name, json.dumps(task))
            logging.info('{0} {1}'.format(length, task))
            self.stat.incr(self.stat_hash, 'gen_task')


# 机构募资事件详情-base
class SmtInfoBaseProjectGenerator(BasicGenerator):
    def get_project(self):
        sql1 = '''select qkid from smt_manage_fund_survey_info;'''
        sql2 = '''select qkid from smt_manage_fund_survey_base;'''
        rows1 = self.mdb.query(sql1)
        rows2 = self.mdb.query(sql2)
        list = []
        for row in rows1:
            list.append(row)
        for row in rows2:
            list.remove(row)
        return list

    def gen_task(self, extra=None):
        projects = self.get_project()
        for project_id in projects:
            url = 'https://app.pedata.cn/PEDATA_APP_BACK/orgBasic/baseInfo?qkid=' + str(project_id[0])
            task = {
                'type': self.name,
                'url': url,
                'project_id': project_id[0],
            }
            length = self.rds.rpush(self.queue_name, json.dumps(task))
            logging.info('{0} {1}'.format(length, task))
            self.stat.incr(self.stat_hash, 'gen_task')
